
import sun.reflect.generics.tree.Tree;

import java.util.*;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2022/1/18
 * \* Time: 9:13
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */

class TreeNode {
    public char val;
    public TreeNode left;//左孩子的引用
    public TreeNode right;//右孩子的引用
    public TreeNode(char val) {
        this.val = val;
    }
}
public class BinaryTree {
    //public BTNode root;//二叉树的根节点

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }

    // 前序遍历
    void preOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //shift+f6->回车才会生效

    public List<Character> preorderTraversal(TreeNode root) {
        List<Character> retlist = new ArrayList<>();
        if(root == null) {
            return retlist;
        }
        retlist.add(root.val);
        List<Character> leftTree = preorderTraversal(root.left);
        retlist.addAll(leftTree);

        List<Character> rightTree =preorderTraversal(root.right);
        retlist.addAll(rightTree);

        return retlist;
    }

    // 中序遍历
    void inOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    /*public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> retlist = new ArrayList<>();
        if(root == null) {
            return retlist;
        }
        List<Integer> leftTree = inorderTraversal(root.left);
        retlist.addAll(leftTree);

        retlist.add(root.val);


        List<Integer> rightTree =inorderTraversal(root.right);
        retlist.addAll(rightTree);

        return retlist;
    }*/

    // 后序遍历
    void postOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

    /*public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> retlist = new ArrayList<>();
        if(root == null) {
            return retlist;
        }
        List<Integer> leftTree = postorderTraversal(root.left);
        retlist.addAll(leftTree);



        List<Integer> rightTree =postorderTraversal(root.right);
        retlist.addAll(rightTree);


        retlist.add(root.val);


        return retlist;
    }*/
    int count = 0;
    int size1(TreeNode root){

        if(root == null) {
            return 0;
        }
        count++;
        preOrder(root.left);
        preOrder(root.right);
        return count;
    }

    int size2(TreeNode root){
        if(root == null) return 0;
        return size2(root.left) + size2(root.right) + 1;
    }
    public int leaves;
    public int  getLeafNodeCount1(TreeNode root){
        if(root == null) return 0;
        if(root.left == null && root.right == null){
            leaves++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
        return leaves;
    }

    public int getLeafNodeCount2(TreeNode root){
        if(root == null)return 0;
        if(root.left == null && root.right == null){
            return 1;
        }else {
            return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
        }

    }
    public int getKLevelNodeCount(TreeNode root,int k){
        if(root == null) return 0;
        if(k == 1)return 1;
        return getKLevelNodeCount(root.left,k-1) + getKLevelNodeCount(root.right,k-1);

    }

    public int getHeight(TreeNode root){
        if(root == null)return 0;
        return Math.max(getHeight(root.left),getHeight(root.right))+1;
    }
    TreeNode find(TreeNode root,char val){
        if(root == null) return null;
        if(root.val == val) return root;
        TreeNode tmp = find(root.left,val);
        if(tmp != null){
            return tmp;
        }
        return find(root.right, val);
    }

    boolean isCompleteTree(TreeNode root){
        if(root == null) return true;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        TreeNode cur;
        while(queue.peek() != null){
            cur = queue.poll();
            if(cur != null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }
        }
        while(!queue.isEmpty()){
            cur = queue.poll();
            if(cur != null){
                break;
            }
        }
        return queue.isEmpty();
    }

    public void levelOrder(TreeNode root) {

    }
}